package com.jcl.recruit.console.api.login;

import com.jcl.core.resp.ApiResponse;
import com.jcl.recruit.console.login.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.AuthenticationException;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName UserController
 * @Author yyliu
 * @Date 2018/12/18 11:00
 * @Version 1.0
 **/
@CrossOrigin
@RestController
//@RequestMapping(value = "/console", produces = "text/html;charset=UTF-8")
@RequestMapping(value = "/console", produces = "application/json;charset=UTF-8")
public class LoginController {

    @Autowired
    private LoginService loginService;

    /**
     * 用户登录
     *
     * @param username 用户名
     * @param password 密码
     * @return 操作结果
     * @throws AuthenticationException 错误信息
     */
    @PostMapping(value = "/auth/login", params = {"username", "password"})
    public ApiResponse login(String username, String password) throws AuthenticationException {

        Map<String, Object> dataMap = new HashMap<>();
        String token = loginService.login(username, password);
        dataMap.put("token", token);
        return ApiResponse.success(dataMap);
    }


    /**
     * 刷新密钥
     *
     * @param authorization 原密钥
     * @return 新密钥
     * @throws AuthenticationException 错误信息
     */
    @GetMapping(value = "/auth/refreshToken")
    public ApiResponse refreshToken(@RequestHeader String authorization) throws AuthenticationException {
        Map<String, Object> dataMap = new HashMap<>();
        String token = loginService.refreshToken(authorization);
        dataMap.put("token", token);
        return ApiResponse.success(dataMap);
    }


}
